//
//  MMTextPreviewViewController.m
//  MemeMaker
//
//  Created by dangthan on 7/22/13.
//  Copyright (c) 2013 soyo. All rights reserved.
//

#import "MMTextPreviewViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "MMTextStyleModel.h"

@interface MMTextPreviewViewController () {
    MMTextStyleViewController *textStyleVC;
}
@property (weak, nonatomic) IBOutlet UIButton *btnCancel;
@property (weak, nonatomic) IBOutlet UIButton *btnApply;
@property (weak, nonatomic) IBOutlet UIButton *btnStyle;

@end

@implementation MMTextPreviewViewController
@synthesize delegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    NSInteger minus = 0;
    if (!SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
        minus = -STATUS_BAR_HEIGHT;
    }
    
    CGRect frame1 = self.btnCancel.frame;
    frame1.origin.y += minus;
    self.btnCancel.frame = frame1;
    
    CGRect frame2 = self.btnApply.frame;
    frame2.origin.y += minus;
    self.btnApply.frame = frame2;
    
    CGRect frame3 = self.btnStyle.frame;
    frame3.origin.y += minus;
    self.btnStyle.frame = frame3;
    
    CGRect frame4 = self.txtPreview.frame;
    frame4.origin.y += minus;
    self.txtPreview.frame = frame4;
    
    [_txtPreview setDelegate:self];
    _txtPreview.layer.borderColor = [UIColor blackColor].CGColor;
    _txtPreview.layer.borderWidth = 1;
    [_txtPreview setTextAlignment:NSTextAlignmentCenter];
    gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    styleModel = [[MMTextStyleModel alloc] init];
}

- (void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [_txtPreview becomeFirstResponder];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
    _txtPreview = nil;
    self.btnStyle = nil;
    self.btnApply = nil;
    self.btnCancel = nil;
    [self setTxtPreview:nil];
    [super viewDidUnload];
}


#pragma mark - IBAction
- (IBAction)buttonClicked:(id)sender {
    UIButton *btn = (UIButton*)sender;
    if (btn.tag == 1) {
        //close and use old
        if (delegate && [delegate respondsToSelector:@selector(didApplyWithString:color:font:andAlign:)]) {
            [delegate didApplyWithString:nil color:nil font:nil andAlign:NSTextAlignmentLeft];
        }
        
        [self dismissViewControllerAnimated:YES completion:nil];
    } else if (btn.tag == 2) {
        //edit Style
        if (!textStyleVC) {
            textStyleVC = [[MMTextStyleViewController alloc] initWithNibName:@"MMTextStyleViewController" bundle:nil];
            [textStyleVC setDelegate:self];
            [textStyleVC setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
        }
        [self presentViewController:textStyleVC animated:YES completion:nil];
    
    } else { //save pattern
        if ([[MMUtils trimString:_txtPreview.text] length]) {
            if (delegate && [delegate respondsToSelector:@selector(didApplyWithString:color:font:andAlign:)]) {
                [self dismissViewControllerAnimated:YES completion:nil];
                [delegate didApplyWithString:_txtPreview.text color:styleModel.pColor font:styleModel.pFont andAlign:styleModel.pTextAlignment];
            }
        } else {
            UIAlertView *alrInput = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"warning", @"") message:NSLocalizedString(@"emptytext", @"") delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", @"") otherButtonTitles:nil, nil];
            [alrInput show];
        }
    }
}

- (void) hideKeyboard {
    [self.view removeGestureRecognizer:gesture];
    [MMUtils hideKeyboard:self.view completion:^(BOOL finished) {
        
    }];
    [MMUtils hideKeyboard:self.view];
    [_txtPreview setFrame:CGRectMake(_txtPreview.frame.origin.x, _txtPreview.frame.origin.y, 300.0, 390.0)];
}

#pragma mark - TextViewDelegate
- (void) textViewDidBeginEditing:(UITextView *)textView {
    [self.view addGestureRecognizer:gesture];
    [_txtPreview setFrame:CGRectMake(_txtPreview.frame.origin.x, _txtPreview.frame.origin.y, 300.0, 180.0)];
}


#pragma mark - TextStyleViewController Delegate
- (void) didFinishedChanged:(MMTextStyleModel *)textStyleModel {
    styleModel = textStyleModel;
    [_txtPreview setTextColor:textStyleModel.pColor];
    [_txtPreview setFont:textStyleModel.pFont];
    [_txtPreview setTextAlignment:textStyleModel.pTextAlignment];
}


@end
